Starting from Scratch: Basic Web Application
We'll cover the following
In this part, we will walk you through getting a basic web application running in the cloud on AWS. We will start with a blank project, and build the application and its infrastructure step by step. Each step focuses on a single aspect of the infrastructure, and we will try to explain in detail what’s happening, and why.
All the source code shown in this guide is available on GitHub. Each commit represents a code checkpoint from the book, and you can find it all here: AWS Bootstrap.
When interacting with AWS, we will use both the AWS console and the AWS CLI. In addition, we will also make use of the following tools:
• GitHub as the source code repository for our application and infrastructure code.
• node.js and npm to build our application.
• git for version control.
• curl to interact with our application.
In this section, we will create a tiny web application and we will get it running on an EC2 instance in an AWS account. We will start by performing all the steps manually, and we will automate them in later sections. Our application is not very interesting, and the way it will be hosted is far from ideal, but it will allow us to spend the rest of this book building a well-contained AWS setup, step by step.
Creating our application#
We will need
git
andnpm
installed. But don’t worry, these are already installed on our platform. So you can straight away use them in our terminal widget.
Now, let’s create our bare-bones application, along with a git repository to store it.
Our application is going to listen for HTTP requests on port 8080 and respond with “Hello World”. The entire application will be in one file, server.js
.
⚠️ Line #4: we’ll run the server on port
8080
because port numbers below1024
require root privileges.
We can run our application directly with the node
command.
And we can test it with curl
from another terminal window.
All these commands are already executed in the below widget. Hit on the RUN button to start the server.
/
- server.js
Next, let’s use a process manager to monitor our application so that it automatically restarts if it crashes. To do this, we need to modify our package.json
file.
Line #11: Takes a dependency on pm2, a node process manager.
Line #7: From now on, we’ll use npm start to start our application via pm2.
Line #7: pm2 will monitor it under the name hello_aws and send its stdout to ../logs/app.log
Line #8: We’ll use npm stop to tell pm2 to stop our application.
Line #9: A dummy build step. Your actual production build process goes here.
Now, we need to use npm to get the new dependency we added in package.json
.
In package.json
we specified that the application’s logs are sent to ../logs
. Having the directory for logs outside the application’s directory will be important when we deploy this with CodeDeploy, since it prevents the logs directory from being deleted with every deployment. For now, let’s create the directory manually on our local machine.
And now we should be able to start our application through the process manager.
Testing via curl
should once again show our “Hello World” message.
Again, all these commands are already executed in the widget below. Hit on the RUN button to start the server.
/
- server.js
And with everything working, we can now commit all our changes to git.
Pushing our code to GitHub#
If you don’t already have a GitHub account, create one (it’s free for simple projects like this).
Next, create a new GitHub repository. We named ours aws-bootstrap
. The repository needs to be public for now, but we’ll be able to make it private once we set up CodeBuild.
Now that we have a GitHub repository, let’s push our local changes to GitHub.
Line #1: replace <username>
with your GitHub username.
Again all these git commands are executed. You just need to set your Github username as the environment variable first.
NOTE: When you run the code, it will ask you your username and password before pushing the files to Github. So don’t miss that!
/
- server.js
In the next lesson, we will host our application and manually install it on an EC2 instance.